package com.hongxun.modules.csg.controller;

import java.util.List;
import java.util.Map;

import com.hongxun.common.validator.ValidatorUtils;
import com.hongxun.common.validator.group.AddGroup;
import com.hongxun.common.validator.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.csg.entity.CBasicsWorkEntity;
import com.hongxun.modules.csg.service.CBasicsWorkService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 隐蔽工程：设备基础
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-08-30 15:00:41
 */
@RestController
@RequestMapping("/csg/cbasicswork")
public class CBasicsWorkController {
	@Autowired
	private CBasicsWorkService cBasicsWorkService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
		if(query.get("mCGuid")!=null&&!query.get("mCGuid").toString().equals(""))
		{
			wheres+=" and  m_c_guid = '" + query.get("mCGuid").toString()+"' ";
		}
		if(query.get("state")!=null&&query.get("state").toString().equals("isdelno")) {
			wheres += " and  b_w_datastate != '删除' ";
		}
		query.put("filterSql",wheres+" ");

		List<CBasicsWorkEntity> cBasicsWorkList = cBasicsWorkService.queryList(query);
		int total = cBasicsWorkService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(cBasicsWorkList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}




	@RequestMapping("/select")
	public R select(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}

		wheres+=" and  b_w_datastate != '删除' ";
		query.put("filterSql",wheres+" ");
		List<CBasicsWorkEntity> cBasicsWorkEntityList = cBasicsWorkService.queryList(query);
		return R.ok().put("returnlist", cBasicsWorkEntityList);
	}






	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		CBasicsWorkEntity cBasicsWork = cBasicsWorkService.queryObject(guid);
		
		return R.ok().put("cBasicsWork", cBasicsWork);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody CBasicsWorkEntity cBasicsWork){
		cBasicsWork.setBWDatastate("新增");
		cBasicsWorkService.save(cBasicsWork);
		
		return R.ok();
	}

	/**
	 * 新增设备基础
	 * @param cBasicsWork
	 * @param guid
	 * @return
	 */
	@RequestMapping("/saveNew/{mCGuid}/{pGuid}")
	public R save(@RequestBody CBasicsWorkEntity cBasicsWork ,
				  @PathVariable("mCGuid") String guid ,
				  @PathVariable("pGuid") String pGuid)
	{
		ValidatorUtils.validateEntity(cBasicsWork, AddGroup.class);
		cBasicsWork.setMCGuid(guid);
		cBasicsWork.setPGuid(pGuid);
		cBasicsWork.setMCName("设备基础");
		cBasicsWork.setBWDatastate("新增");
		cBasicsWorkService.save(cBasicsWork);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody CBasicsWorkEntity cBasicsWork){
		ValidatorUtils.validateEntity(cBasicsWork, UpdateGroup.class);
		cBasicsWork.setBWDatastate("修改");
		cBasicsWorkService.update(cBasicsWork);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		cBasicsWorkService.deleteBatch(guids);
		
		return R.ok();
	}
	
}
